<template>
	<xn-form-container
		:title="formData.id ? '编辑库存明细' : '增加库存明细'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="门店ID：" name="storeId">
                            <xn-page-select
                                ref="xnstoreIdPageSelectRef"
                                v-model:value="formData.storeId"
                                placeholder="请选择门店ID"
                                allow-clear
                                show-search
                                :page-function="storeIdSelector"
                            />
			</a-form-item>
			<a-form-item label="仓库ID：" name="storageId">
                            <xn-page-select
                                ref="xnstorageIdPageSelectRef"
                                v-model:value="formData.storageId"
                                placeholder="请选择仓库ID"
                                allow-clear
                                show-search
                                :page-function="storageIdSelector"
                            />
			</a-form-item>
			<a-form-item label="商品ID：" name="storeProductionId">
                            <xn-page-select
                                ref="xnstoreProductionIdPageSelectRef"
                                v-model:value="formData.storeProductionId"
                                placeholder="请选择商品ID"
                                allow-clear
                                show-search
                                :page-function="storeProductionIdSelector"
                            />
			</a-form-item>
			<a-form-item label="商品库存：" name="storeStock">
				<a-input v-model:value="formData.storeStock" placeholder="请输入商品库存" allow-clear />
			</a-form-item>
			<a-form-item label="库位：" name="storeStorageLocation">
                            <xn-page-select
                                ref="xnstoreStorageLocationPageSelectRef"
                                v-model:value="formData.storeStorageLocation"
                                placeholder="请选择库位"
                                allow-clear
                                show-search
                                :page-function="storeStorageLocationSelector"
                            />
			</a-form-item>
			<a-form-item label="批次：" name="storeBatch">
				<a-input v-model:value="formData.storeBatch" placeholder="请输入批次" allow-clear />
			</a-form-item>
			<a-form-item label="供应商：" name="storeSupplier">
                            <xn-page-select
                                ref="xnstoreSupplierPageSelectRef"
                                v-model:value="formData.storeSupplier"
                                placeholder="请选择供应商"
                                allow-clear
                                show-search
                                :page-function="storeSupplierSelector"
                            />
			</a-form-item>
			<a-form-item label="数据归属：" name="companyId">
				<a-input v-model:value="formData.companyId" placeholder="请输入数据归属" allow-clear />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="supplierStocksForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import supplierStocksApi from '@/api/supplier/warehouse/supplierStocksApi'
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
        const xnstoreIdPageSelectRef = ref();
        const storeIdSelector = (param) => {
            return supplierStoresApi.supplierStoresPage(param).then((data) => {
                if (data.records){
                    data.records = data.records.map((item) => {
                        return {
                            name: item.storeName,
                            id: item.id,
                        };
                    });
                }
                return Promise.resolve(data)
            })
        }
        const xnstorageIdPageSelectRef = ref();
        const storageIdSelector = (param) => {
            return supplierStoragesApi.supplierStoragesPage(param).then((data) => {
                if (data.records){
                    data.records = data.records.map((item) => {
                        return {
                            name: item.storeName,
                            id: item.id,
                        };
                    });
                }
                return Promise.resolve(data)
            })
        }
        const xnstoreProductionIdPageSelectRef = ref();
        const storeProductionIdSelector = (param) => {
            return supplierProductionsApi.supplierProductionsPage(param).then((data) => {
                if (data.records){
                    data.records = data.records.map((item) => {
                        return {
                            name: item.productionName,
                            id: item.id,
                        };
                    });
                }
                return Promise.resolve(data)
            })
        }
        const xnstoreStorageLocationPageSelectRef = ref();
        const storeStorageLocationSelector = (param) => {
            return supplierStoresLocationPage(param).then((data) => {
                if (data.records){
                    data.records = data.records.map((item) => {
                        return {
                            name: item.locationCode,
                            id: item.id,
                        };
                    });
                }
                return Promise.resolve(data)
            })
        }
        const xnstoreSupplierPageSelectRef = ref();
        const storeSupplierSelector = (param) => {
            return supplierSuppliersApi.supplierSuppliersPage(param).then((data) => {
                if (data.records){
                    data.records = data.records.map((item) => {
                        return {
                            name: item.supplierName,
                            id: item.id,
                        };
                    });
                }
                return Promise.resolve(data)
            })
        }

	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
                nextTick(() => {
                    xnstoreIdPageSelectRef.value.onPage({current: 1});
                });
                nextTick(() => {
                    xnstorageIdPageSelectRef.value.onPage({current: 1});
                });
                nextTick(() => {
                    xnstoreProductionIdPageSelectRef.value.onPage({current: 1});
                });
                nextTick(() => {
                    xnstoreStorageLocationPageSelectRef.value.onPage({current: 1});
                });
                nextTick(() => {
                    xnstoreSupplierPageSelectRef.value.onPage({current: 1});
                });
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
		storeId: [required('请输入门店ID')],
		storageId: [required('请输入仓库ID')],
		storeProductionId: [required('请输入商品ID')],
		storeStock: [required('请输入商品库存')],
		storeSupplier: [required('请输入供应商')],
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				supplierStocksApi
					.supplierStocksSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
